package omm.domain.evolutiongraph;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;

/*******
 * @deprecated POR REQUERIMIENTOS DEL USUARIO
 * @author jbatllori
 *
 */


public class TrimestralPeriodStrategy implements PeriodStrategy {
	final private int MONTHS = 3;
	final private Integer periods;
	final private Map<Integer, String> trimestralMap;

	public TrimestralPeriodStrategy() {
		periods = 4;
		trimestralMap = new HashMap<Integer, String>();
		// Numero de trimestre y numero de mes en el que empieza
		trimestralMap.put(1,"1T");
		trimestralMap.put(4,"2T");
		trimestralMap.put(7,"3T");
		trimestralMap.put(10,"4T");
	}

	@Override
	public Map<DateTime, Double> calculatePeriods() {
		Map<DateTime, Double> periodsBars = new TreeMap<DateTime, Double>(
				DateTimeComparator.getDateOnlyInstance());
		// Carga de fechas
		DateTime actualTime = new DateTime(System.currentTimeMillis());
		int actualMonth = actualTime.getMonthOfYear();
		int actualYear = actualTime.getYear();
		for (Map.Entry<Integer, String> e : trimestralMap.entrySet()) {
			int month = e.getKey();
			if ((month <= actualMonth) && (actualMonth - month < MONTHS)) {
				DateTime itTime = new DateTime(actualYear, month, 1, 0, 0, 0);
				periodsBars.put(itTime, 0.0);
				int year = actualYear;
				for (int i = 0; i < trimestralMap.size()-1; i++) {
					if ( month <= MONTHS){
						month = 12 - (MONTHS - month);
						year -= 1;
					}else {
						month -= MONTHS;
					}
					itTime = new DateTime(year,month, 1, 0, 0, 0);
					periodsBars.put(itTime, 0.0);
				}

				break;
			}
		}

		return periodsBars;
	}

	@Override
	public Integer calculateBarsWidth() {
		return (int) (380 / (periods + 1));
	}

	@Override
	public boolean isInPeriod(DateTime modifTime, DateTime dt) {
		int initialMonth = dt.getMonthOfYear();
		int initialYear = dt.getYear();
		// Esta en periodo si modifTime esta entre los 3 meses siguientes a dt
		if ((initialYear == modifTime.getYear()) && (initialMonth <= modifTime.getMonthOfYear())
				&& (modifTime.getMonthOfYear() - initialMonth) < 3)
			return true;
		return false;
	}

	@Override
	public String calculatePeriodsNames(Map<DateTime, Double> periodsValue) {
		String periodsNames = "";
		for (Map.Entry<DateTime, Double> e : periodsValue.entrySet()) {
			DateTime dt = e.getKey();
			Integer month = dt.getMonthOfYear();
			Integer year = dt.getYear();
			periodsNames = periodsNames + trimestralMap.get(month) + '/' + year.toString().substring(2) + '|';
			}
		return periodsNames.substring(0, periodsNames.length() - 1);
	}

	@Override
	public Integer calculateBarsSeparation() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTitle() {
		return "trimestral";
	}

	@Override
	public DateTime getBegin() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DateTime getEnd() {
		// TODO Auto-generated method stub
		return null;
	}

}
